﻿<style type="text/css">
.ext-el-mask{z-index:20000;position:fixed;top:0;left:0;-moz-opacity:0.5;opacity:.50;filter:alpha(opacity=50);background-color:#CCC;width:100%;height:100%;zoom:1;}
.ext-el-mask-msg{z-index:20001;position:absolute;top:0;left:0;border:1px solid #6593cf;background:#c3daf9 url(images/tb-blue.gif) repeat-x 0 -16px;padding:2px;}
.ext-el-mask-msg div{padding:5px 10px 5px 10px;background:#eee;border:1px solid #a3bad9;color:#333;font:normal 12px tahoma,arial,helvetica,sans-serif;cursor:wait;}
.x-mask-loading div{padding:5px 10px 5px 25px;background:#eee url(images/cloading.gif) no-repeat 5px 5px;line-height:16px;}
#loading-msg{position:fixed;top:45%!important;left:43%!important;z-index:20001!important;}
</style>
<div id="loading-mask" class="ext-el-mask" style="display: none;"></div>
<div id="loading-msg" class="ext-el-mask-msg x-mask-loading" style="display: none;">
  <div>Processing...</div>
</div>
<script type='text/javascript'>
function numeric_only(e)
{
	var key;	var keychar;
	if (window.event)
   key = window.event.keyCode;
	else if (e)
   key = e.which;
	else	return true;
	
	keychar = String.fromCharCode(key);
	
	if ((key==null) || (key==0) || (key==8) || (key==9) || (key==13) || (key==27))	return true;
	else if ((("0123456789").indexOf(keychar) > -1))	return true;
	else	return false;
}

function calc_hours(id)
{
	var scaled_hours = 0;
	var total_hours = 0;	
	var ratio = { "late_shift":1.2, "weekend":1.5, "late_weekend":2	};	
	for (var x=0;x<4;x++)
	{
		for (var y=0;y<7;y++)
		{
			if ($("#s_"+id+"_"+x+"_"+y+":checked").val())
			{
				if (x==0)
				{
					total_hours += 8;
					if (y < 5)
						scaled_hours += 8 * ratio["late_shift"];
					else
						scaled_hours += 8 * ratio["late_weekend"];
				}
				else if (x==3)
				{
					total_hours += 6;
					if (y < 5)
						scaled_hours += 6 * ratio["late_shift"];
					else
						scaled_hours += 6 * ratio["late_weekend"];
				}
				else
				{
					total_hours += 4;
					if ((y < 5) || ((y==5) && (x==1)))
						scaled_hours += 4;
					else
						scaled_hours += 4 * ratio["weekend"];
				}			
			}					
		}
	}
	$("#total_hours_"+id).val(total_hours);
	$("#scaled_hours_"+id).val(Math.ceil(scaled_hours*10)/10);	
}

function calc_salary(id)
{	
	if ($("#salary_type_"+id+":checked").val())
	{		
		$("#salary_week_"+id).val($("#total_hours_"+id).val()*$("#salary_shift_"+id).val()/4);
		$("#salary_month_"+id).val(Math.ceil($("#salary_week_"+id).val()*4.4));
	}
	else
	{		
		$("#salary_week_"+id).val(Math.round($("#salary_month_"+id).val()/4.4));		
		$("#salary_shift_"+id).val(Math.round($("#salary_week_"+id).val()*4/$("#total_hours_"+id).val()));
	}
}

function change_st(id)
{
	if ($("#salary_type_"+id+":checked").val())
	{
		$("#salary_shift_"+id).attr("disabled", false);
		$("#salary_month_"+id).attr("disabled", true);		
	}
	else
	{
		$("#salary_shift_"+id).attr("disabled", true);
		$("#salary_month_"+id).attr("disabled", false);
	}
}
</script>
<?php
/* configuration */
$whmcs_path = "whmcs";
$ajax_url = "http://".$_SERVER["HTTP_HOST"]."/".$whmcs_path."/modules/admin/shift_manager/ajax.php";

/* basic authentication check */
if (!isset($_SESSION["adminid"]) || !defined('ROOTDIR'))
	exit("<div id=infobox>Access Denied!</div>");

/* clean up deleted admin account */
$r = mysql_query("SELECT adminid FROM mod_schedule WHERE adminid NOT IN (SELECT id FROM tbladmins);");
if (mysql_num_rows($r))
{
  while ($admin = mysql_fetch_assoc($r))
  {
    mysql_query("DELETE FROM mod_schedule WHERE adminid = '".$admin["adminid"]."';");
  }
}

/* set up newly created admin account */
$r = mysql_query("SELECT id FROM tbladmins WHERE id NOT IN (SELECT adminid FROM mod_schedule);");
if (mysql_num_rows($r))
{
  while ($admin = mysql_fetch_assoc($r))
  {
    mysql_query("INSERT INTO mod_schedule (adminid, schedule, priority) VALUES ('".$admin["id"]."', NULL, NULL);");
  }
}

/* check for admin account with edit admin roles privilege */	
if (!mysql_num_rows(mysql_query("SELECT permid FROM tbladmins JOIN tbladminperms ON tbladmins.roleid = tbladminperms.roleid WHERE permid=64 AND tbladmins.id=". intval($_SESSION["adminid"]))))
{
	$cond = "(tbladmins.id=".intval($_SESSION["adminid"]).")";
	$editable = 0;
}
else
{
  $cond = 1;
	$editable = 1;
}
	
$r = mysql_query("SELECT tbladmins.id as id, tbladmins.lastname as lastname, tbladmins.firstname as firstname, mod_schedule.schedule as schedule, mod_schedule.priority as priority, mod_schedule.salarytype as salarytype, mod_schedule.salary as salary FROM tbladmins LEFT JOIN mod_schedule ON tbladmins.id = mod_schedule.adminid WHERE ".$cond." ORDER BY mod_schedule.priority ASC;");

echo "<div class='tablebg'>
			<table class='datatable' width='100%' border='0' cellspacing='1' cellpadding='3'>
				<tr id='tblh'>";
if ($editable) echo "<th>&nbsp;#&nbsp;</th><th>Operator</th>";

echo "<th>Weekly schedule</th>
			<th>Absense/Overtime</th>
			<th>Salary</th>
			</tr>";
				
while ($admin = mysql_fetch_assoc($r))
{
  echo "<tr id='r".$admin["id"]."'>";	
	if ($editable) echo "<td style='padding:5px;'><img src='images/moveup.gif' class='up' id='".$admin["id"]."'></td><td style='padding:5px;'>".$admin["lastname"]." ".$admin["firstname"]."</td>";
	echo "<td><center>
						<table cellspacing=1 cellpadding=3 bgcolor='#cccccc' style='margin-bottom:5px;'>
						<tr bgcolor='#efefef' style='text-align:center;font-weight:bold'>
							<td width='40'>Shift</td>
							<td width='40'>Mon</td>
							<td width='40'>Tue</td>
							<td width='40'>Wed</td>
							<td width='40'>Thu</td>
							<td width='40'>Fri</td>
							<td width='40'>Sat</td>
							<td width='40'>Sun</td>
						</tr>";
	
	/* schedule block */
  for ($x=0;$x<4;$x++)
  {
    echo "	<tr bgcolor='#ffffff' style='text-align:center;'>
							<td>".($x+1)."</td>";
    for ($y=0;$y<7;$y++)
    {
      echo "	<td><input type='checkbox' onchange='calc_hours(".$admin["id"].")' id='s_".$admin["id"]."_".$x."_".$y."' name='s_".$admin["id"]."_".$x."_".$y."' value='1'";
      if ($admin["schedule"]!=NULL && in_array(($x+$y*4),explode(",", $admin["schedule"])))
				echo " checked";
			if (!$editable)
				echo " disabled";
		}
      echo "></td></tr>";
  }
	
  echo "</table>		
				<b>Working hours: </b>
				<input type='text' class='resform' id='total_hours_".$admin["id"]."' size='2' value='' disabled />&nbsp;&nbsp;
				<b>Paid hours: </b>
				<input type='text' class='resform' id='scaled_hours_".$admin["id"]."' size='2' value='' disabled />";
	if ($editable)
		echo "&nbsp;&nbsp;<input type=submit value='Save' onclick='updateSchedule(".$admin["id"].");' />";
	echo"</center>
			</td>";

	/* overtime block */
	echo "<td style='padding:5px;'><center>";
	if ($editable)
	{
	}
	else
	{
		echo "You have free 12 days of absence left.<br/>";
		//$query = mysql_query("SELECT * FROM mod_dayoff ORDER BY date DESC");
		//if (mysql_num_rows($query))
		echo "<input type='button' value='Request of absence' onClick=''/>
		<div>
		<input type='text' name='absencedate' value='' class='datepick'>
		</div>";
	}
	echo "</center></td>";
	
  /* salary block */	 
  echo "<td style='padding:5px;'>";
	if ($editable)
	{
		echo "<center>You can choose 1 of 2 following options					
					<table>
						<tr>
							<td><input type='radio' name='choose_".$admin["id"]."' id='salary_type_".$admin["id"]."' onclick='change_st(".$admin["id"].")'";
		if ($admin["salarytype"] == 1) 
		{
			echo " checked ";
			$salary = $admin["salary"];
		}
		else
		{
			$salary = round($admin["salary"]*44)/10;
		}
		echo "/></td>
							<td>Per shift</td>
							<td><input type='text' size='15' maxlength='15'  name='salary_shift_".$admin["id"]."' id='salary_shift_".$admin["id"]."' onKeyUp='calc_salary(".$admin["id"].")' onKeyPress='return numeric_only(event);'";
		if ($admin["salarytype"] == 1) 
			echo " value='".$salary."'";
		else
			echo " disabled ";
			
		echo "/></td>
						</tr>
						<tr>
							<td>&nbsp</td>
							<td>Per week</td>
							<td><input type='text' class='resform' name='salary_week_".$admin["id"]."' id='salary_week_".$admin["id"]."' size='15' disabled /></td>
						</tr>
		        <tr>
							<td><input type='radio' name='choose_".$admin["id"]."' onclick='change_st(".$admin["id"].")'";
		if ($admin["salarytype"] == 0) 
			echo " checked ";
		echo "/></td>
							<td>Per month </td>
							<td><input type='text' size='15' maxlength='15' name='salary_month_".$admin["id"]."' id='salary_month_".$admin["id"]."' onKeyUp='calc_salary(".$admin["id"].")' onKeyPress='return numeric_only(event);'";
		if ($admin["salarytype"] == 0)
			echo " value='".$salary."'";
		else
			echo " disabled ";
		echo "/></td>
						</tr>
					</table>
					<input type='submit' value='Save' onClick='updateSalary(".$admin["id"].");' />
					</center>";
	}
	else
	{
		if ($admin["salarytype"] == 1) 
		{
			echo "Your salary is <b>".number_format($admin["salary"]*1000)."</b> VND per shift.<br>";
		}
		else
		{
			echo "Your salary is <b>".number_format($admin["salary"]*1000)."</b> VND per month.";
		}
	}
	echo "</td></tr>
	<script>calc_hours(".$admin["id"].");";
	if ($editable) echo "calc_salary(id);";
	echo "</script>";
}

echo "</table></div>";
/*
if (!$editable)
{
	echo "<br/><h2>Dayoff</h2>
		  <a onClick='show_pr()'>previous request</a>&nbsp;&nbsp;&nbsp;&nbsp;                     
		  <a onClick='show_ar()'>absence request</a>
	      <div id='dayoff'></div>";
}
else
{
	echo "<br/><h2>Dayoff</h2>";
	$query = mysql_query("SELECT * FROM mod_dayoff ORDER BY date DESC,shiftid DESC");
	if(mysql_num_rows($query))
	{
		echo "<div id='dayoff'><table border=1 cellspacing=0 cellpadding=2>
			  <tr bgcolor='#efefef' style='font-weight:bold;text-align:center'>
				  <td width=100>Date</td>
				  <td>Shift</td>
				  <td width=150>Admin</td>
				  <td width=200>Reason</td>
				  <td width=150>Replacement</td>
				  <td>Status</td>
				  <td>Action</td></tr>";
		while($temp = mysql_fetch_assoc($query))
		{
			$_temp = mysql_fetch_assoc(mysql_query("SELECT id,firstname,lastname FROM tbladmins WHERE id = ".$temp["adminid"]));
			echo "<tr style='text-align:center'>
				  <td><a onClick='filter(\"date\",\"".$temp["date"]."\")'>".$temp["date"]."</a></td>
				  <td>".$temp["shiftid"]."</td>
				  <td><a onClick='filter(\"adminid\",".$_temp["id"].")'>".$_temp["lastname"]." ".$_temp["firstname"]."</a></td>
				  <td>".$temp["reason"]."</td>
				  <td>".$temp["replacement"]."</td>
				  <td><a onClick='filter(\"status\",\"".$temp["status"]."\")'>".$temp["status"]."</a></td>
				  <td><a onClick='delRequest(".$temp["adminid"].",\"".$temp["date"]."\",".$temp["shiftid"].");window.location.reload();'>delete</a>";
			if($temp["status"] == "pending")
			{
				echo "&nbsp;<a onClick='rejectRequest(".$temp["adminid"].",\"".$temp["date"]."\",".$temp["shiftid"].");window.location.reload();'>reject</a>&nbsp;<a onClick='approveRequest(".$temp["adminid"].",\"".$temp["date"]."\",".$temp["shiftid"].");window.location.reload();'>approve</a>";
			}
			echo "</td></tr>";
		}
		echo "</table></div><center><input type='button' value='all' onClick='window.location.reload()'/></center>";
	}
	else
	{
		echo "Nothing";
	}

}
*/
?>